<style>
	@import "/static/css/common.css";
	@import "/static/css/index-list.css";
</style>
<template>
	<view class="contact">
		<scroll-view class="friend-list" ref="friendList" scroll-y>
			<view class="friend-item" v-for="(chat, key) in friendlist" :key="key" @click="talk" :data-id="chat.id">
				<image class="avatar" :src="chat.avatar ? `${imgUrl}${chat.avatar}` : '/static/img/userAvatar.jpg'"
					mode="aspectFill"></image>
				<view class="friend-info">
					<view class="friend-name">{{chat.nickname}}</view>
				</view>
			</view>
		</scroll-view>
		<Footer></Footer>
		<view v-if="tip" class="tip-layer">
			<view class="tip">{{tipContent}}</view>
		</view>
	</view>
</template>

<script>
	import Footer from '@/pages/footer/index.vue';
	import {
		showTips
	} from '@/static/js/common.js';

	export default {
		components: {
			Footer,
		},
		data() {
			return {
				httpUrl: "https://www.fengjian123.com/service9501/",
				imgUrl: "https://www.fengjian123.com/",
				// httpUrl: "http://192.168.200.128:9501/",
				// imgUrl: "http://192.168.200.128:8080/",
				username: '',
				token: '',
				friendlist: [],
				tipContent: '',
				tip: 0,
			}
		},
		onLoad(option) {
			this.getChat();
		},
		methods: {
			getChat: function() {
				let _this = this;
				uni.getStorage({
					key: 'cookie',
					success: function(res) {
						if (res.data.id) {
							if (res.data.login) {
								_this.username = res.data.username;
								uni.request({
									url: _this.httpUrl + 'friend/friendlist',
									header: {
										'Content-Type': 'application/json',
										'Authorization': 'Bearer ' + res.data.token,
									},
									success: (res) => {
										console.log(res.data.msg);
										if (res.data.err == 0) {
											_this.friendlist = JSON.parse(res.data.friendList);
										} else {
											uni.reLaunch({
												url: '/pages/index/index',
											});
											// 账号密码版
											// _this.tips(res.data.msg);
											// setTimeout(() => {uni.reLaunch({url: '/pages/login/index',});}, 1000)
										}
									},
									fail: (res) => {
										console.log("登录失败，请检查网络");
										_this.tips('网络不好，请检查网络..');
									},
								});
							} else {
								_this.toLogin();
								// setTimeout(() => {uni.reLaunch({url: '/pages/login/index',});}, 1000)  // 账号密码版
							}
						} else {
							_this.tips('网络不好，请检查网络..'); // _this.tips('登录失效，正在跳转...');  // 账号密码版
							// setTimeout(() => {uni.reLaunch({url: '/pages/login/index',});}, 1000)  // 账号密码版
						}
					},
					fail: function() {
						_this.toLogin();
						// uni.reLaunch({url: '/pages/login/index',});  // 账号密码版
					}
				});
			},
			talk: function(e) {
				let _this = this;
				let dataid = e.currentTarget.dataset.id;
				if (dataid) {
					uni.setStorageSync('friendId', dataid);
					uni.navigateTo({
						url: '/pages/chat/chating',
					});
				} else {
					console.log("获取friendId失败");
				}
			},
			toLogin() {
				uni.showModal({
					title: '提示',
					content: '请登录..',
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/login/wx-index',
							})
						} else if (res.cancel) {
							uni.reLaunch({
								url: '/pages/index/index',
							});
						}
					}
				});
				return;
			},
			tips: function(content) {
				showTips(this, content);
			}
		}
	}
</script>